package cn.iocoder.yudao.module.system.controller.admin.appversion;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.appversion.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.appversion.AppVersionDO;
import cn.iocoder.yudao.module.system.service.appversion.AppVersionService;

@Tag(name = "管理后台 - App版本控制")
@RestController
@RequestMapping("/system/app-version")
@Validated
public class AppVersionController {

    @Resource
    private AppVersionService appVersionService;

    @PostMapping("/create")
    @Operation(summary = "创建App版本控制")
    public CommonResult<Long> createAppVersion(@Valid @RequestBody AppVersionSaveReqVO createReqVO) {
        return success(appVersionService.createAppVersion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新App版本控制")
    public CommonResult<Boolean> updateAppVersion(@Valid @RequestBody AppVersionSaveReqVO updateReqVO) {
        appVersionService.updateAppVersion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除App版本控制")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteAppVersion(@RequestParam("id") Long id) {
        appVersionService.deleteAppVersion(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得App版本控制")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppVersionRespVO> getAppVersion(@RequestParam("id") Long id) {
        AppVersionDO appVersion = appVersionService.getAppVersion(id);
        return success(BeanUtils.toBean(appVersion, AppVersionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得App版本控制分页")
    public CommonResult<PageResult<AppVersionRespVO>> getAppVersionPage(@Valid AppVersionPageReqVO pageReqVO) {
        PageResult<AppVersionDO> pageResult = appVersionService.getAppVersionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppVersionRespVO.class));
    }

    @GetMapping("/getOne")
    @Operation(summary = "获得该平台最新的")
    public CommonResult<AppVersionRespVO> getOne(String platform) {
        AppVersionDO pageResult = appVersionService.getOne(platform);
        return success(BeanUtils.toBean(pageResult, AppVersionRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得App版本控制不分页")
    public CommonResult<List<AppVersionRespVO>> getAppVersionList(@Valid AppVersionPageReqVO pageReqVO) {
        List<AppVersionDO> pageResult = appVersionService.getAppVersionList(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppVersionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出App版本控制 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAppVersionExcel(@Valid AppVersionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AppVersionDO> list = appVersionService.getAppVersionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "App版本控制.xls", "数据", AppVersionRespVO.class,
                        BeanUtils.toBean(list, AppVersionRespVO.class));
    }

}